package com.realgodo.site.service.space;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.space.Message;
import com.realgodo.site.entity.space.Space;

@Service
@Transactional
public class MessageManager extends DefaultEntityManager<Message, Long> {

	@Autowired
	protected SessionFactory sessionFactory;

	@Autowired
	private SpaceManager spaceManager;

	public Page<Message> findMessages(Page<Message> page, Long receiverId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Message.class);
		c.add(Restrictions.or(Restrictions.eq("receiver.id", receiverId),
				Restrictions.eq("sender.id", receiverId)));
		c.add(Restrictions.isNull("subject"));
		c.addOrder(Order.desc("createDate"));

		return this.entityDao.findByCriteria(page, c);
	}

	/**
	 * 查找主题下的邮件列表
	 * 
	 * @param receiverId
	 * @param subjectId
	 * @return
	 */
	public List<Message> findSubjectMessages(Long receiverId, Long subjectId) {
		Page<Message> page = new Page<Message>(50);
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Message.class);
		c.add(Restrictions.or(Restrictions.eq("id", subjectId), Restrictions
				.eq("subject.id", subjectId)));
		c.addOrder(Order.desc("createDate"));

		return this.entityDao.findByCriteria(page, c).getResult();
	}

	/**
	 * 获取新站内信数量
	 * 
	 * @param mySpace
	 * @return
	 */
	public Long getNewMessageCount(Space mySpace) {
		Session session = this.sessionFactory.getCurrentSession();
		String hql = "select count(o) from Message o where o.status = "
				+ Message.NOT_READ + " and o.receiver.id = " + mySpace.getId()
				+ "and o.subject is null";
		Query q = session.createQuery(hql);
		return (Long) q.uniqueResult();
	}

	/**
	 * 回复站内信
	 * 
	 * @param space
	 * @param title
	 * @param content
	 * @param messageId
	 * @return
	 */
	public boolean reply(Space space, String title, String content,
			Long messageId) {

		// 更新就邮件状态
		Message old = this.get(messageId);
		old.setStatus(Message.NOT_READ);
		this.saveMerge(old);

		Space oldSpace = null;
		if (old.getSender() != null && old.getReceiver() != null) {
			if (old.getSender().getId().equals(space.getId())) {
				oldSpace = old.getReceiver();
			} else {
				oldSpace = old.getSender();
			}
		}
		Message message = new Message();
		message.setSubject(old);
		message.setStatus(Message.NOT_READ);
		message.setContent(content);
		message.setTitle(title);
		message.setSender(space);
		message.setReceiver(oldSpace);
		return (this.saveMerge(message) != null) ? true : false;

	}

	/**
	 * 主动发送邮件
	 * 
	 * @param sender
	 * @param title
	 * @param content
	 * @param receiverId
	 */
	public void send(Space sender, String title, String content, Long receiverId) {
		Space receiver = spaceManager.get(receiverId);
		Message message = new Message();
		message.setStatus(Message.NOT_READ);
		message.setContent(content);
		message.setTitle(title);
		message.setSender(sender);
		message.setReceiver(receiver);
		this.save(message);
	}
}
